/*
 * FileStream.java
 * 
 * Created on 26/Nov/2007, 23:47:26
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IO;

import Dicionario.Dicionario;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Pedro
 */
public class FileStream implements IFileStream{
    

   
    public ArrayList LeListaTermos(String path) {

        ArrayList<String> listaTermos = new ArrayList<String>();
        try {
            FileReader in = new FileReader(path);
            BufferedReader bf = new BufferedReader(in);
            String line;
            boolean fim = false;
            while (!fim) {

                line = bf.readLine();
                if (line == null) {
                    fim = true;
                } else {
                    //  System.out.println(line);
                    listaTermos.add(line);
                }
            }
            in.close();
            // System.out.println(listaTermos.toString());
        } catch (IOException e) {

            System.out.println("Erro a tentar abrir o ficheiro da lista de termos!\n");
        }
        return listaTermos;
    }

    public String LeTexto(String path) {
        
        String str = new String();

        try {
            File f = new File(path);
            FileReader rd = new FileReader(f);
            char[] buf = new char[(int) f.length()];
            rd.read(buf);
            str = new String(buf);
    
       // matcher = pattern.matcher(inputStr);
       // matchFound = matcher.matches();
    } catch (IOException e) {
    }
        
        /*
        try {
        BufferedReader in = new BufferedReader(new FileReader(path));
        
        while ((text = in.readLine()) != null) {
          System.out.println(str.concat(text));
           
        }
        in.close();
        System.out.println(str + "ola");
    } catch (IOException e) {  }
     */
         System.out.println(str.length());
    return str;
    }

    public void EscreveDicionario(Dicionario d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void EscreveTermosEncontrados(ArrayList t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void EscreveTexto(String path, String dados) {
        try {
            File f = new File(path);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            FileWriter out = new FileWriter(f);
            out.write(dados);
            out.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void EscreveTexto(String path, ArrayList dados) {
        try {
            File f = new File(path);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            FileWriter out = new FileWriter(f);
            String fim =dados.toString();
            out.write(fim);
            
//            String a[] =new String[40];
//            a = fim.split("\\,");
//            int tam = a.length;
//            for(int i=0; i<tam; i++)
//                out.write(a[i]);
            
            out.close();
      
        } catch (IOException e) {
            System.err.println(e);
        }
        
    }


}
